package com.zts.modules.sys.controller;

import com.zts.base.BasicResourceAssembler;
import com.zts.base.page.PageBound;
import com.zts.base.resource.ListResource;
import com.zts.modules.sys.entity.Para;
import com.zts.modules.sys.entity.model.ParaModel;
import com.zts.modules.sys.entity.resource.ParaResource;
import com.zts.modules.sys.service.ParaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.ExposesResourceFor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by jisd on 2017/6/19.
 */
@RestController("paraController")
@RequestMapping("/v1/paras")
@Api(value = "paras",description = "系统参数相关API")
@ExposesResourceFor(ParaResource.class)
public class ParaController {
    private static Logger logger = LoggerFactory.getLogger(ParaController.class);
    @Autowired
    ParaService paraService;
    private BasicResourceAssembler<Para, ParaResource> paraAssembler =
            new BasicResourceAssembler(ParaController.class, ParaResource.class);



    /**
     * 查询全部系统参数
     *
     * @param paraModel
     * @param pageBound
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "getParas", httpMethod = "GET", value = "获取系统参数的分页信息")
    public final ListResource<ParaResource> getParas(@ApiParam(required = false, value = "系统参数的查询条件")
                                                       @ModelAttribute("paraModel") ParaModel paraModel,
                                                       PageBound pageBound) {
        List<Para> list = paraService.getList(paraModel.transEntity(), pageBound);
        return paraAssembler.toListResource(list);
    }


    /**
     * 根据组的id，获取唯一的系统参数。
     * *
     * @param
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "getPara", httpMethod = "GET", value = "根据主键id查询参数")
    public final ParaResource getPara(@ApiParam(required = true, value = "主键id")
                                        @PathVariable("id") Long id) {
        return paraAssembler.toResource(paraService.get(id));
    }

    /**
     * 添加特定系统参数
     *
     * @param paraModel
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = {""}, method = {RequestMethod.POST},
            consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE},
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "addPara", httpMethod = "POST", value = "新增系统参数")
    public final ParaResource insertPara(@ApiParam(required = true, value = "资源信息，接收数据格式为json") @RequestBody ParaModel paraModel) {
        Para para = paraService.insert(paraModel.transEntity());
        return paraAssembler.toResource(para);
    }

    /**
     * 根据主键Id更新特定系统参数
     *
     * @param id
     * @param paraModel
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = {"/{id}"}, method = {RequestMethod.PUT},
            consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE},
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "updatePara", httpMethod = "PUT", value = "根据主键id修改系统参数")
    public final int updatePara(@ApiParam(required = true, value = "资源主键id") @PathVariable("id") Long id,
                                    @ApiParam(required = true, value = "要修改的系统参数信息项及对应修改的值，接收数据格式为json")
                                    @RequestBody ParaModel paraModel) {
        logger.debug("updatePara param：{}", paraModel.toString());
        paraModel.setId(id);
        return paraService.update(paraModel.transEntity());
    }

    /**
     * 根据id删除特定系统资源
     *
     * @param id
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.NO_CONTENT)
    @RequestMapping(value = {"/{id}"}, method = {RequestMethod.DELETE}, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "deletePara", httpMethod = "DELETE", value = "根据主键id删除系统参数")
    public final int deletePara(@ApiParam(required = true, value = "系统参数主键id") @PathVariable("id") Long id) {
        Para para = new Para();
        para.setId(id);
        logger.info("id=====" + id);
        return paraService.delete(para);
    }



}
